package top.cdmrx.demo.ucenter.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.cdmrx.common.R;
import top.cdmrx.common.utils.JwtUtils;
import top.cdmrx.demo.ucenter.entity.Member;
import top.cdmrx.demo.ucenter.entity.vo.RegisterVo;
import top.cdmrx.demo.ucenter.service.MemberService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 登录方法
     * @param member
     * @return
     */
    @PostMapping("login")
    public R loginUser(@RequestBody Member member){
        String token = memberService.loginUser(member);
        return R.success().data("token",token);
    }

    /**
     * 注册方法
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.registerUser(registerVo);

        return R.success();
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);

        Member member = memberService.getById(memberIdByJwtToken);

        return R.success().data("userInfo",member);

    }
}

